<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >

    <h:body>



        <ui:composition template="/resources/template/template.xhtml">

            <ui:define name="content">

                <h:form>

                    <p:panel >
                        <f:facet name="header" >
                            <div>
                                <h:outputLabel value="Visit of #{patientEncounterController.current.patient.person.nameWithTitle} on " >
                                </h:outputLabel>
                                <h:outputLabel value="#{patientEncounterController.current.createdAt}" >
                                    <f:convertDateTime pattern="dd MMMM yyyy" ></f:convertDateTime>
                                </h:outputLabel>
                                <h:outputLabel value= " by #{patientEncounterController.current.opdDoctor.person.nameWithTitle}" >
                                </h:outputLabel>
                                <p:spacer width="10" ></p:spacer>
                                <h:panelGrid columns="6">
                                    <p:commandButton ajax="false" value="Save Visit" action="#{patientEncounterController.saveSelected()}"  onclick="onSubmitButton();" ></p:commandButton>
                                    <p:commandButton ajax="false" value="Pharmacy Issue" action="#{patientEncounterController.issueItems()}"  onclick="onSubmitButton();"></p:commandButton>
                                    <p:commandButton ajax="false" value="Services/Investigation" action="#{patientEncounterController.issueServices()}"  onclick="onSubmitButton();"></p:commandButton>

                                    <p:commandButton ajax="false" value="Back to search" action="clinical_patient_for_receptionist"  style="float: right;" onclick="onSubmitButton();" ></p:commandButton>
                                    <p:commandButton ajax="false" value="Back to queue" action="clinical_queue" style="float: right;" onclick="onSubmitButton();" ></p:commandButton>

                                    

                                    <p:commandButton ajax="false" value="Back to Previous Visit" 
                                                     disabled="#{patientEncounterController.startedEncounter eq patientEncounterController.current}"
                                                     action="#{patientEncounterController.backToStartingEncounter()}" style="float: right;" ></p:commandButton>



                                </h:panelGrid>

                            </div>
                        </f:facet>



                        <h:panelGrid columns="2" styleClass="alignTop" >

                            <h:panelGroup>
                                <p:panel styleClass="alignTop" >
                                    <f:facet name="header" >
                                        <h:outputText value="Prescreption" >

                                        </h:outputText>
                                        <p:spacer width="30" height="1" ></p:spacer>
                                        <p:commandButton ajax="false" value="Print" >
                                            <p:printer target="txtComments" ></p:printer>
                                        </p:commandButton>
                                    </f:facet>

                                    <p:inputTextarea id="txtComments" 
                                                     queryDelay="0"
                                                     style="width: 420px;height: 300px;" 
                                                     value="#{patientEncounterController.current.comments}" 
                                                     completeMethod="#{patientEncounterController.completeClinicalComments}" minQueryLength="1"
                                                     >
                                        <f:ajax event="keyup" execute="txtComments" ></f:ajax>
                                    </p:inputTextarea>
                                    <p:watermark forElement="txtComments" value="Write comments here." ></p:watermark>
                                </p:panel>
                                <p:panel header="Diagnosis" styleClass="alignTop" >
                                    <h:panelGrid columns="1" styleClass="alignTop" >

                                        <h:panelGrid columns="1" styleClass="alignTop" >
                                            <p:autoComplete id="acDx" value="#{patientEncounterController.diagnosis}"
                                                            completeMethod="#{diagnosisController.completeDiagnosis}"
                                                            var="dx" forceSelection="true"
                                                            converter="diagnosisConverter"
                                                            itemLabel="#{dx.name}" 
                                                            itemValue="#{dx}"

                                                            >
                                            </p:autoComplete>
                                            <p:commandButton ajax="false" id="btnAddDx" value="Add Diagnosis" 
                                                             action="#{patientEncounterController.addDx()}" 
                                                             process="acDx txtDx" 
                                                             update="tblDx" onclick="onSubmitButton();" >
                                            </p:commandButton>
                                            <p:inputTextarea 
                                                id="txtDx" 
                                                style="width: 90%;"
                                                value="#{patientEncounterController.diagnosisComments}" ></p:inputTextarea>

                                            <p:watermark value="Select Diagnosis." for="acDx" ></p:watermark>
                                            <p:watermark value="Write Details about the Diagnosis here." for="txtDx" ></p:watermark>

                                        </h:panelGrid>

                                        <p:dataTable id="tblDx" value="#{patientEncounterController.current.diagnosis}" var="edx" >
                                            <p:column >
                                                <h:outputLabel value="#{edx.clinicalFindingItem.name}" ></h:outputLabel>
                                            </p:column>
                                            <p:column >
                                                <h:outputLabel value="#{edx.lobValue}" ></h:outputLabel>
                                            </p:column>
                                            <p:column  >
                                                <p:commandButton ajax="false" value="X" update="tblDx" action="#{patientEncounterController.removeCfv()}" onclick="onSubmitButton();"> 
                                                    <f:setPropertyActionListener value="#{edx}" target="#{patientEncounterController.removingCfv}" />
                                                </p:commandButton>
                                            </p:column>

                                        </p:dataTable>





                                    </h:panelGrid>

                                </p:panel>

                            </h:panelGroup>
                            <h:panelGroup>

                                <h:panelGrid columns="3" >

                                    <p:panel header="Patient Details" >



                                        <h:panelGrid columns="2" id="panelPt" >

                                            <h:outputLabel value="Name" ></h:outputLabel>
                                            <p:inputText autocomplete="off" id="txtPersonName" value="#{patientEncounterController.current.patient.person.name}" ></p:inputText>
                                            <h:outputLabel value="Date of Birth" ></h:outputLabel>
                                            <p:calendar id="calPersonDob" value="#{patientEncounterController.current.patient.person.dob}" pattern="dd MMMM yyyy" navigator="true" ></p:calendar>
                                            <h:outputLabel value="Address" ></h:outputLabel>
                                            <h:inputTextarea id="txtPatientAddress"  value="#{patientEncounterController.current.patient.person.address}" ></h:inputTextarea>

                                            <h:outputLabel value="Age" ></h:outputLabel>
                                            <h:outputLabel value="#{patientEncounterController.current.patient.age}" ></h:outputLabel>
                                            <h:outputLabel value="Picture" ></h:outputLabel>
                                            <h:outputLabel value="#{patientEncounterController.current.patient.baImage}" ></h:outputLabel>
                                            <h:outputLabel value="Sex" ></h:outputLabel>
                                            <h:outputLabel value="#{patientEncounterController.current.patient.person.sex}" ></h:outputLabel>
                                            <h:outputLabel value="Patient Comments" ></h:outputLabel>
                                            <h:inputTextarea id="txtPatientComments" value="#{patientEncounterController.current.patient.comments}" ></h:inputTextarea>


                                            <p:commandButton ajax="false" value="Update Patient" actionListener="#{patientEncounterController.updatePerson()}"
                                                             process="@this txtPersonName calPersonDob txtPatientComments txtPatientAddress" update="panelPt" onclick="onSubmitButton();" ></p:commandButton>

                                        </h:panelGrid>

                                    </p:panel>

                                    <p:panel header="Investigations" styleClass="alignTop" >
                                        <p:dataTable  id="tblBills" value="#{patientEncounterController.currentPatientInvestigations}" var="pi"  rows="5"
                                                      paginator="true"
                                                      paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                                      >

                                            <p:column  headerText="Ix" styleClass="alignTop" >
                                                <h:outputLabel id="lblBillIx" value="#{pi.billItem.item.name}" >
                                                </h:outputLabel>
                                                <p:tooltip for="lblBillIx"  >
                                                    <h:outputLabel value="Bill No : #{pi.billItem.bill.insId}" ></h:outputLabel>
                                                    <br/>
                                                    <h:outputLabel value="Billed From : #{pi.billItem.bill.institution.name}" ></h:outputLabel>
                                                    <br/>
                                                    <h:outputLabel value="Sampled : " />
                                                    <h:outputLabel rendered="#{pi.collected}" value="#{pi.sampledAt}" >
                                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                                    </h:outputLabel>
                                                    <br/>
                                                    <h:outputLabel value="Received : " />
                                                    <h:outputLabel rendered="#{pi.received}" value="#{pi.receivedAt}" >
                                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                                    </h:outputLabel>
                                                    <br/>
                                                    <h:panelGroup rendered="#{pi.billItem.bill.cancelled}" >
                                                        <h:outputLabel style="color: red;" value="Cancelled : " />
                                                        <h:outputLabel style="color: red;" rendered="#{pi.billItem.bill.cancelled}" value="#{pi.billItem.bill.cancelledBill.createdAt}" >
                                                            <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                                        </h:outputLabel>
                                                    </h:panelGroup>
                                                    <h:panelGroup rendered="#{pi.billItem.refunded}" >
                                                        <h:outputLabel style="color: red;" value="Refunded : " />
                                                        <h:outputLabel style="color: red;" rendered="#{pi.billItem.refunded}" value="#{pi.billItem.referanceBillItem.bill.createdAt}" >
                                                            <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                                        </h:outputLabel>
                                                    </h:panelGroup>


                                                </p:tooltip>
                                            </p:column>


                                            <p:column  headerText="Actions"  styleClass="alignTop noBorderGrid"  >
                                                <h:panelGrid columns="4" style="border: none;" styleClass="noBorderGrid"
                                                             rendered="#{pi.billItem.bill.cancelled ne true and pi.billItem.refunded ne true }" >
                                                    <p:commandLink process="btnSample" update="tblBills"  
                                                                   disabled="#{pi.collected}"   
                                                                   id="btnSample" 
                                                                   action="#{patientInvestigationController.markAsSampled}" >
                                                        <f:setPropertyActionListener value="#{pi}" target="#{patientInvestigationController.current}"></f:setPropertyActionListener>
                                                        <p:graphicImage library="image" styleClass="standedicon" name="bleed.png" ></p:graphicImage>
                                                    </p:commandLink>
                                                    <p:commandLink process="btnReceive" update="tblBills"  disabled="#{!pi.received ne true}"   id="btnReceive" action="#{patientInvestigationController.markAsReceived()}" >
                                                        <f:setPropertyActionListener value="#{pi}" target="#{patientInvestigationController.current}"></f:setPropertyActionListener>
                                                        <p:graphicImage library="image" styleClass="standedicon" name="receive.jpeg" ></p:graphicImage>
                                                    </p:commandLink>
                                                    <p:commandLink ajax="false" id="btnPrintWorksheets" action="/lab/single_worksheet">
                                                        <f:setPropertyActionListener value="#{pi}" target="#{patientInvestigationController.current}" ></f:setPropertyActionListener>
                                                        <p:graphicImage library="image" styleClass="standedicon" name="worksheet.gif" ></p:graphicImage>
                                                    </p:commandLink>
                                                    <p:commandLink id="btnNewReport" ajax="false" disabled="#{!pi.received}" action="/lab/patient_report" actionListener="#{patientReportController.createNewReport(pi)}" >
                                                        <f:setPropertyActionListener value="#{pi}" target="#{patientReportController.currentPtIx}"></f:setPropertyActionListener>
                                                        <p:graphicImage library="image" styleClass="standedicon" name="data_entry.png" ></p:graphicImage>
                                                    </p:commandLink>
                                                </h:panelGrid>

                                                <p:tooltip for="btnReceive"  >
                                                    <h:outputLabel value="Receive Sample" rendered="#{!pi.received}" ></h:outputLabel>
                                                    <h:outputLabel value="Sample received at " rendered="#{pi.received}" ></h:outputLabel>
                                                    <h:outputLabel value="#{pi.receivedAt}" rendered="#{pi.received}" >
                                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                                    </h:outputLabel>
                                                </p:tooltip>

                                                <p:tooltip for="btnSample" value="Collect Sample" >
                                                    <h:outputLabel value="Collect Sample" rendered="#{!pi.collected }" ></h:outputLabel>
                                                    <h:outputLabel value="Sampled at " rendered="#{pi.collected}" ></h:outputLabel>
                                                    <h:outputLabel value="#{pi.sampledAt}" rendered="#{pi.collected}" >
                                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                                    </h:outputLabel>
                                                </p:tooltip>

                                                <p:tooltip for="btnNewReport" value="New Report" ></p:tooltip>

                                                <p:tooltip for="btnPrintWorksheets" value="Print worksheet" ></p:tooltip>

                                                <h:dataTable  rendered="#{!empty itemForItemController.getItemsForParentItem(pi.investigation)}"
                                                              value="#{itemForItemController.getItemsForParentItem(pi.investigation)}" var="ifi" >
                                                    <h:column >
                                                        <p:commandLink disabled="#{!pi.received}" value="New #{ifi.name}" action="#{patientReportController.enterNewReportFormat(pi, ifi)}" 
                                                                       ajax="false">
                                                        </p:commandLink>
                                                    </h:column>
                                                </h:dataTable>

                                            </p:column>

                                            <p:column headerText="Data-Entered Reports"  styleClass="alignTop noBorderGrid" >

                                                <h:dataTable value="#{pi.patientReports}" var="dpr" rendered="#{pi.patientReports.size() ne 0}"  styleClass="noBorderGrid" >
                                                    <h:column >

                                                        <h:panelGrid columns="6" styleClass="noBorderGrid">

                                                            <p:commandLink id="cmdOldReport" style="text-decoration:underline;" 
                                                                           value="#{dpr.item.name}" 
                                                                           ajax="false" action="/lab/patient_report">
                                                                <f:setPropertyActionListener value="#{dpr}" target="#{patientReportController.currentPatientReport}"></f:setPropertyActionListener>
                                                            </p:commandLink>

                                                            <p:commandLink id="cmdOldReportEdit" ajax="false" action="/lab/patient_report">
                                                                <f:setPropertyActionListener value="#{dpr}" target="#{patientReportController.currentPatientReport}"></f:setPropertyActionListener>
                                                                <p:graphicImage library="image" styleClass="standedicon" name="data_entry.png" ></p:graphicImage>
                                                            </p:commandLink>

                                                            <p:commandLink id="cmdOldReportPrint" ajax="false" action="/lab/patient_report_print">
                                                                <p:graphicImage library="image" name="print.jpg" styleClass="standedicon" ></p:graphicImage>
                                                                <f:setPropertyActionListener value="#{dpr}" target="#{patientReportController.currentPatientReport}"></f:setPropertyActionListener>
                                                            </p:commandLink>

                                                            <p:graphicImage id="imgApproved" library="image" name="approved_icon.png" 
                                                                            style="height: 30px; width: 30px; #{dpr.approved ? '':'opacity: 0.1;'}" ></p:graphicImage>
                                                            <p:graphicImage id="imgPrinted" library="image" name="print_ok.png" 
                                                                            style="height: 30px; width: 30px;#{dpr.printed ? '':'opacity: 0.1;'}" ></p:graphicImage>
                                                        </h:panelGrid>


                                                        <p:tooltip for="imgApproved" >
                                                            <h:outputLabel value="Approved" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                                            </h:outputLabel>
                                                        </p:tooltip>
                                                        <p:tooltip for="imgPrinted" >
                                                            <h:outputLabel value="Printed" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                                            </h:outputLabel>
                                                        </p:tooltip>
                                                        <p:tooltip for="cmdOldReportPrint" >
                                                            <h:outputLabel value="To Print" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                                            </h:outputLabel>
                                                        </p:tooltip>
                                                        <p:tooltip for="cmdOldReport cmdOldReportEdit" >
                                                            <h:outputLabel value="To Edit, Approve or Print" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                                            </h:outputLabel>
                                                            <br/>
                                                            <h:outputLabel value="#{dpr.item.name}" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                                            </h:outputLabel>
                                                            <br/>
                                                            <h:outputLabel value="Data enterd at " ></h:outputLabel>
                                                            <h:outputLabel value="#{dpr.dataEntryAt}" >
                                                                <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                                            </h:outputLabel>
                                                            <br/>
                                                            <h:outputLabel value="Approved at " rendered="#{dpr.approved}" ></h:outputLabel>
                                                            <h:outputLabel value="#{dpr.approveAt}"  rendered="#{dpr.approved}" >
                                                                <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                                            </h:outputLabel>
                                                            <br/>
                                                            <h:outputLabel value="Printed at "  rendered="#{dpr.printed}" ></h:outputLabel>
                                                            <h:outputLabel value="#{dpr.printingAt}"  rendered="#{dpr.printed}" >
                                                                <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                                            </h:outputLabel>
                                                        </p:tooltip>


                                                    </h:column>
                                                </h:dataTable>


                                            </p:column>

                                        </p:dataTable>
                                    </p:panel>

                                    <p:panel header="Channellings" >
                                        <p:dataTable value="#{patientEncounterController.currentChannelBills}" 
                                                     var="bill" rows="5"
                                                     paginator="true"
                                                     paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                                     >
                                            <p:column headerText="No" >
                                                <h:outputLabel value="#{bill.insId}"></h:outputLabel>
                                            </p:column>
                                            <p:column headerText="Date/Time" >
                                                <h:outputLabel  value="#{bill.singleBillSession.serviceSession.sessionDate}" >
                                                    <f:convertDateTime pattern="yyyy MM dd" ></f:convertDateTime>
                                                </h:outputLabel>
                                                <h:outputLabel value="#{bill.singleBillSession.serviceSession.startingTime}" >
                                                    <f:convertDateTime pattern="hh:mm a" ></f:convertDateTime>
                                                </h:outputLabel>
                                            </p:column>
                                            <p:column  headerText="Bill Type"   >
                                                <h:outputLabel value="#{bill.billType}" ></h:outputLabel>
                                            </p:column>
                                            <p:column  headerText="Consultant"  >
                                                <h:outputLabel value="#{bill.staff.person.name}" ></h:outputLabel>
                                            </p:column>

                                            <p:column  headerText="Bill Fees"   >

                                                <table >
                                                    <ui:repeat value="#{bill.billFees}" var="tbi" >
                                                        <tr>
                                                            <td>
                                                                <p:outputLabel value="#{tbi.fee.name}" ></p:outputLabel>
                                                            </td>
                                                            <td>
                                                                <p:outputLabel value="#{tbi.feeValue}" ></p:outputLabel>
                                                            </td>
                                                        </tr>
                                                    </ui:repeat>
                                                </table>


                                            </p:column>
                                            <p:column headerText="Net Value" >
                                                <h:outputLabel value="#{bill.netTotal}" >
                                                    <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                </h:outputLabel>
                                            </p:column>
                                        </p:dataTable>






                                    </p:panel>

                                    <p:panel header="Past Visits" styleClass="alignTop" >
                                        <p:dataTable value="#{patientEncounterController.currentPatientEncounters}" var="pe" rows="5"
                                                     paginator="true"
                                                     paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                                     >

                                            <p:column headerText="Visit Date" >
                                                <h:outputLabel value="#{pe.createdAt}" >
                                                    <f:convertDateTime pattern="dd MMMM yyyy" ></f:convertDateTime>
                                                </h:outputLabel>
                                            </p:column>
                                            <p:column headerText="Dx" >
                                                <ui:repeat value="#{pe.diagnosis}" var="dxs" >
                                                    <h:outputLabel value="#{dxs.stringValue}" ></h:outputLabel>
                                                    <h:outputLabel value="&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;" rendered="#{dxs.lobValue ne null}"></h:outputLabel>
                                                    <h:outputLabel value="#{dxs.lobValue}" ></h:outputLabel>
                                                    <br/>
                                                </ui:repeat>
                                            </p:column>
                                            <p:column headerText="View" >
                                                <p:commandButton ajax="false" value="View" action="#{patientEncounterController.prepareToDisplayPastVisit()}" onclick="onSubmitButton();" >
                                                    <f:setPropertyActionListener target="#{patientEncounterController.encounterToDisplay}" value="#{pe}" ></f:setPropertyActionListener>
                                                </p:commandButton>
                                            </p:column>
                                        </p:dataTable>
                                    </p:panel>

                                    <p:panel header="Billings" >
                                        <p:dataTable id="tblPtBills"
                                                     value="#{patientEncounterController.currentPatientBills}" 
                                                     var="bill" rows="5"
                                                     paginator="true"
                                                     paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                                     >
                                            <p:column headerText="No" >
                                                <h:outputLabel value="#{bill.insId}" id="lblBillNos"  ></h:outputLabel>
                                            </p:column>
                                            <p:column headerText="Date/Time" >
                                                <h:outputLabel id="lblBillDates" value="#{bill.createdAt}" >
                                                    <f:convertDateTime parent="dd-MMM-yyyy hh:mm a" ></f:convertDateTime>
                                                </h:outputLabel>
                                            </p:column>
                                            <p:column  headerText="Bill Type" id="colType"  >
                                                <h:outputLabel id="lblBillTypes" value="#{bill.billType}" ></h:outputLabel>

                                            </p:column>

                                            <p:column  headerText="Bill Type" id="colType1"  >

                                                <table >
                                                    <ui:repeat value="#{bill.billItems}" var="tbi" >
                                                        <tr>
                                                            <td>
                                                                <p:outputLabel value="#{tbi.item.name}" ></p:outputLabel>
                                                            </td>
                                                            <td>
                                                                <p:outputLabel value="#{tbi.qty}" rendered="#{bill.billType eq 'PharmacySale'}" ></p:outputLabel>
                                                            </td>
                                                        </tr>
                                                    </ui:repeat>
                                                </table>


                                            </p:column>
                                            <p:column headerText="Net Value" >
                                                <h:outputLabel value="#{bill.netTotal}" >
                                                    <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                </h:outputLabel>
                                            </p:column>
                                        </p:dataTable>






                                    </p:panel>

                                </h:panelGrid>

                            </h:panelGroup>
                        </h:panelGrid>
                    </p:panel>

                </h:form>


            </ui:define>
        </ui:composition>

    </h:body>
</html>
